var test;
Ext.define('NIM.view.AssetDataMaster.AssetHeaderForm', {
      extend : 'Ext.form.Panel',
      alias : 'widget.AssetHeaderForm',
      id : 'assetheaderform',
      method: 'POST',
      url: './Services/Asset/SiteMaster/Update',
//      layout : 'fit',
      items : [
       {
            xtype : 'fieldset',
            columnWidth : 0.5,
            title : 'Asset Header Form',
            // collapsible: true,
            width : 690,
            margin : '0 10 10 10',
            defaultType : 'textfield',
            // defaults: {anchor: '100%'},
            layout : {
              type : 'table',
              columns : 2
            },
            items : [{
                  xtype : 'panel',
                  border : false,
                  items : [
               {
                    xtype : 'textfield',
                    cls : 'black',
                    id : 'site_id',
                    name : 'SITE_ID',
                    // emptyText: 'Input Main Category',
                    margin : '15 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Site ID',
                    msgTarget : 'title',
                    allowBlank : false,
                    validateBlank : false
                      // allowOnlyWhitespace: false
                },
                {
                    xtype : 'hiddenfield',
                    cls : 'black',
                    id : 'site_id_temp',
                    name : 'SITE_ID_TEMP',
                    // emptyText: 'Input Main Category',
                    margin : '15 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Site ID',
                    msgTarget : 'title',
                    allowBlank : false,
                    validateBlank : false
                      // allowOnlyWhitespace: false
                },
                {
                    xtype : 'textareafield',
                    grow : true,
                    id : 'address',
                    name : 'ADDRESS',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    margin : '10 10 10 10',
                    fieldLabel : 'Address'

                  }, 
              {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'propinsi',
                    name : 'PROPINSI',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Propinsi',
                    msgTarget : 'title',
                    allowBlank : true, 
                    displayField: 'nama_prov',
                	valueField: 'id_prov',
                    store: 'AssetDataMaster.Provinsi',
                    listeners: {
                    	select: function(obj, newValue, oldValue, eOpts) {
                    		var valKey = Ext.getCmp("propinsi").getValue();
	                    	Ext.getStore('AssetDataMaster.Kabupaten').removeAll();
                            Ext.getStore('AssetDataMaster.Kabupaten').getProxy().url = './Services/DataMaster/Kabupaten/List?propinsi=' + valKey;
                            Ext.getStore('AssetDataMaster.Kabupaten').load();
                        }
                    }

                      // allowOnlyWhitespace: false
                    }, {
                        xtype : 'combobox',
                        cls : 'black',
                        id : 'kabupaten',
                        name : 'KABUPATEN',
                        // emptyText: 'Input Sub Category 2',
                        margin : '10 10 10 10',
                        labelWidth : 100 / 1366 * screen.width,
                        width : 300 / 1366 * screen.width,
                        fieldLabel : 'Kabupaten',
                        msgTarget : 'title',
                        allowBlank : true, 
                        displayField: 'nama_kabkot',
                        store: 'AssetDataMaster.Kabupaten',
                    	valueField: 'id_kabkot',
                        listeners: {
                        	select: function(obj, newValue, oldValue, eOpts) {
                    			var valKey = Ext.getCmp("kabupaten").getValue();
    	                    	Ext.getStore('AssetDataMaster.Kecamatan').removeAll();
                                Ext.getStore('AssetDataMaster.Kecamatan').getProxy().url = './Services/DataMaster/Kecamatan/List?kabupaten=' + valKey;
                                Ext.getStore('AssetDataMaster.Kecamatan').load();
                            }
                        }

                          // allowOnlyWhitespace: false
                        }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'kecamatan',
                    name : 'KECAMATAN',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Kecamatan',
                    msgTarget : 'title',
                    allowBlank : true,
                    displayField: 'nama_kec',
                    store: 'AssetDataMaster.Kecamatan',
                	valueField: 'id_kec',
                    listeners: {
                    	select: function(obj, newValue, oldValue, eOpts) {
                    		var valKey = Ext.getCmp("Kecamatan").getValue();
	                    	Ext.getStore('AssetDataMaster.Kelurahan').removeAll();
                            Ext.getStore('AssetDataMaster.Kelurahan').getProxy().url = './Services/DataMaster/Kelurahan/List?kecamatan=' + valKey;
                            Ext.getStore('AssetDataMaster.Kelurahan').load();
                        }
                    }

                      // allowOnlyWhitespace: false
                    },

                  {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'kelurahan',
                    name : 'KELURAHAN',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Kelurahan',
                    msgTarget : 'title',
                    allowBlank : true,
                    displayField: 'nama_kel_lacima',
                    store: 'AssetDataMaster.Kelurahan',
                	valueField: 'id_kel'

                      // allowOnlyWhitespace: false
                    }, {
                    xtype : 'textfield',
                    cls : 'black',
                    id : 'project_id',
                    name : 'PROJECT_ID',
                    // emptyText: 'Input Main Category',
                    margin : '15 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Project ID',
                    msgTarget : 'title',
                    allowBlank : true,
                    validateBlank : false,
                    }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'area',
                    name : 'AREA',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Area',
                    msgTarget : 'title',
                    allowBlank : true,
                    store: 'AssetDataMaster.Area',
                	fieldLabel: 'Area', 
                	valueField: 'area_code',
                	displayField: 'area_description',
                	listeners: {
                    	select: function(obj, newValue, oldValue, eOpts) {
                			var valKey = Ext.getCmp("area").getValue();
                			Ext.getCmp('region').setValue('');
                        	Ext.getStore('AssetDataMaster.Regional').removeAll();
                            Ext.getStore('AssetDataMaster.Regional').getProxy().url = './Services/DataMaster/Region/List?area=' + valKey;
                            Ext.getStore('AssetDataMaster.Regional').load();

                        }
                    }

                      // allowOnlyWhitespace: false
                    }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'site_type',
                    name : 'SITE_TYPE',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Site Type',
                    msgTarget : 'title',
                    allowBlank : true,
                    forceSelection: true,
                    store		: [['ROOFTOP', 'Rooftop'], ['GREENFIELD', 'Greenfield']],
                    }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'msc_include',
                    name : 'MSC_INCLUDE',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'MSC Include',
                    msgTarget : 'title',
                    allowBlank : true,
                    store		: [['YES', 'Yes'], ['NO', 'No']],
                    }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'site_backbone_include',
                    name : 'SITE_BACKBONE_INCLUDE',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Site Backbone',
                    msgTarget : 'title',
                    allowBlank : true,
                    store		: [['YES', 'Yes'], ['NO', 'No']],
                }, 
				
				{
					xtype : 'datefield',
				    cls : 'black',
				    id : 'on_air_date',
					name : 'ON_AIR_DATE',
					minDate : new Date(),
					format : 'Y/m/d',
					fieldLabel : 'DATE',
					labelWidth : 100 / 1366 * screen.width,
					width : 300 / 1366 * screen.width,
				    margin : '10 10 10 10',
				}
			]
                }, 
                {
                  xtype : 'panel',
                  margin : '0 0 0 10',
                  border : false,
                  items : [{
                    xtype : 'textfield',
                    cls : 'black',
                    id : 'site_name',
                    name : 'SITE_NAME',
                    // emptyText: 'Input Main Category',
                    margin : '15 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Site Name',
                    msgTarget : 'title',
                    allowBlank : true,
                    validateBlank : false

                      // allowOnlyWhitespace: false
                    },
                     {
                    xtype : 'textfield',
                    cls : 'black',
                    id : 'kodepos',
                    name : 'KODE_POS',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Kode Pos',
                    msgTarget : 'title',
                    allowBlank : true

                      // allowOnlyWhitespace: false
                    }, {
                    xtype : 'textfield',
                    cls : 'black',
                    id : 'site_latitude',
                    name : 'SITE_LATITUDE',
                    // emptyText: 'Input Main Category',
                    margin : '15 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Latitude',
                    msgTarget : 'title',
                    allowBlank : true,
                    validateBlank : false

                      // allowOnlyWhitespace: false
                    }, {
                    xtype : 'textfield',
                    cls : 'black',
                    id : 'site_longitude',
                    name : 'SITE_LOGITUDE',
                    // emptyText: 'Input Main Category',
                    margin : '15 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Longitude',
                    msgTarget : 'title',
                    allowBlank : true,
                    validateBlank : false

                      // allowOnlyWhitespace: false
                    }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'region',
                    name : 'REGION',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Region',
                    msgTarget : 'title',
                    allowBlank : true, 
                    valueField: 'region_code',
                    displayField: 'region_description',
                	store: 'AssetDataMaster.Regional',
                	fieldLabel: 'Regional'

                      // allowOnlyWhitespace: false
                    }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'bsc_include',
                    name : 'BSC_INCLUDE',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'BSC Include',
                    msgTarget : 'title',
                    allowBlank : true,
                    store		: [['YES', 'Yes'], ['NO', 'No']],
                }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'ttc_include',
                    name : 'TTC_INCLUDE',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'TTC Include',
                    msgTarget : 'title',
                    allowBlank : true,
                    store		: [['YES', 'Yes'], ['NO', 'No']],
                    }, {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'site_simpul_include',
                    name : 'SITE_SIMPUL_INCLUDE',
                    // emptyText: 'Input Sub Category 2',
                    margin : '10 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Site Simpul Include',
                    msgTarget : 'title',
                    allowBlank : true,
                    store		: [['YES', 'Yes'], ['NO', 'No']],
                    }, 
                    {
                    xtype : 'combobox',
                    cls : 'black',
                    id : 'asset_status',
                    name : 'ASSET_STATUS',
                    // emptyText: 'Input Main Category',
                    margin : '15 10 10 10',
                    labelWidth : 100 / 1366 * screen.width,
                    width : 300 / 1366 * screen.width,
                    fieldLabel : 'Asset Status',
                    msgTarget : 'title',
                    allowBlank : true,
                    fieldLabel : 'Asset Status',
                    msgTarget : 'title',
                    allowBlank : true,
                    forceSelection: true,
                    store		: [['ACTIVE', 'Active'], ['INACTIVE', 'Inacvtive']]
                    },
                    {
                 	   xtype : 'panel',
                 	   border : false,
                 	   height: 60,
             	    },    
	                {
	                  xtype : 'panel',
	                  border : false,
	                  colspan:2,
	                  layout:'hbox',
	                  items : [                	   
                       	{
	                        xtype : 'button',
	                        id : 'ButtonAddSite',
	                        margin : '0 10 10 10',
	                        width : 60,
	                        region: 'left',
	                        height:30,
	                        text : 'Update',
	                        handler : function() {
	                       		this.up('form').getForm().submit({     
		                    		waitMsg : 'Processing...',
		                    		success: function(form, action) {
		                    			Ext.Msg.alert('Submited', action.response.responseText);
		                    	    },
		                    	    failure: function(form, action) {
		                    	        Ext.Msg.alert('Submited', action.response.responseText);

                	        		Ext.getStore('AssetDataMaster.SiteList').load();  		
		                    	    }
		                        });
                       		}	
                       	}, 
                       	{
	                        xtype : 'button',
	                        margin : '0 10 10 0',
	                        width :60,
	                        height:30,
	                        text : 'Cancel',
	                        handler : function() {
	                        	Ext.getCmp('assetheaderform').getForm().submit();
	                        }
	                      }
                       	]
	
	                }
                    ]

                }]

          }],
          listeners:{
          	afterrender: function() {
                  Ext.getStore('AssetDataMaster.SiteList').load();   
                //Ext.getStore('AssetDataMaster.Treeview').removeAll();
	    	 		Ext.getStore('AssetDataMaster.Treeview').getProxy().url = './Services/Asset/Site/Categories/'+site_id;
	    	 		Ext.getStore('AssetDataMaster.Treeview').load();
                  
                  	 Ext.getStore('AssetDataMaster.SiteDetails').removeAll();
                   	 Ext.getStore('AssetDataMaster.SiteDetails').getProxy().url = './Services/Asset/SiteDetails?siteid='+site_id;
                  	 Ext.getStore('AssetDataMaster.SiteDetails').load(function(){
                  	 var record = this.getAt(0).raw;
                  	
                  	 
                    Ext.getStore('AssetDataMaster.Regional').removeAll();
                    Ext.getStore('AssetDataMaster.Regional').getProxy().url = './Services/DataMaster/Region/List?area=' + record.area;
                    Ext.getStore('AssetDataMaster.Regional').load();
                    
                    
                  	  Ext.getStore('AssetDataMaster.Kabupaten').removeAll();
                      Ext.getStore('AssetDataMaster.Kabupaten').getProxy().url = './Services/DataMaster/Kabupaten/List?propinsi=' + record.propinsi;
                      Ext.getStore('AssetDataMaster.Kabupaten').load();
                      
                      Ext.getStore('AssetDataMaster.Kecamatan').removeAll();
                      Ext.getStore('AssetDataMaster.Kecamatan').getProxy().url = './Services/DataMaster/Kecamatan/List?kabupaten=' + record.kabupaten;
                      Ext.getStore('AssetDataMaster.Kecamatan').load();
                      
                      Ext.getStore('AssetDataMaster.Kelurahan').removeAll();
                      Ext.getStore('AssetDataMaster.Kelurahan').getProxy().url = './Services/DataMaster/Kelurahan/List?kecamatan=' + record.kecamatan;
                      Ext.getStore('AssetDataMaster.Kelurahan').load();
                      
                      
                      Ext.getCmp('site_id').setValue(record.site_id) ;
                      Ext.getCmp('site_id').setDisabled(false) ;
                      Ext.getCmp('site_name').setValue(record.site_name) ;
                      Ext.getCmp('address').setValue(record.address) ;
                      Ext.getCmp('kodepos').setValue(record.kodepos) ;
                      
                      Ext.getCmp('address').setValue(record.address) ;
                      Ext.getCmp('propinsi').setValue(record.propinsi) ;
                      Ext.getCmp('kabupaten').setValue(record.kabupaten) ;
                      Ext.getCmp('kecamatan').setValue(record.kecamatan) ;
                      Ext.getCmp('kelurahan').setValue(record.kelurahan) ;
                      
                      Ext.getCmp('area').setValue(record.area) ;
                      Ext.getCmp('region').setValue(record.region) ;
                      
                      
                      Ext.getCmp('project_id').setValue(record.project_id) ;
                      Ext.getCmp('site_type').setValue(record.site_type) ;
                      Ext.getCmp('site_latitude').setValue(record.site_latitude) ;
                      Ext.getCmp('site_longitude').setValue(record.site_longitude) ;
                      Ext.getCmp('bsc_include').setValue(record.bsc_include) ;
                      Ext.getCmp('msc_include').setValue(record.msc_include) ;
                      Ext.getCmp('ttc_include').setValue(record.ttc_include) ;
                      Ext.getCmp('site_simpul_include').setValue(record.site_simpul_include) ;
                      Ext.getCmp('site_backbone_include').setValue(record.site_backbone_include) ;
                      Ext.getCmp('on_air_date').setValue(record.on_air_date) ;
                      Ext.getCmp('asset_status').setValue(record.asset_status) ;
                      Ext.getCmp('site_id_temp').setValue(Ext.getCmp('site_id').getValue());
                      //Ext.getCmp('site_infrastructure').setValue(record.get('site_infrastructure')) ;
                      //TODO show detail
                      
                  });
              }
          }

    });